package main;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.setting.Setting;

import java.util.ArrayList;
import java.util.Objects;

/**
 * 初始化类
 *
 * @author hanj.cn@outlook.com
 */
public class Initialize {

    /**
     * 口令
     */
    public static String STOK = "";

    /**
     * 管理员密码
     */
    public static String PASSWORD = "";

    /**
     * 设备码
     */
    public static String DEVICE_CODE = "";

    /**
     * 服务器IP
     */
    public static String IP = "";

    /**
     * 查询时间的间隔
     */
    public static int QUERY_TIME_INTERVAL = -1;

    /**
     * 总时长
     */
    public static int TOTAL_DURATION = -1;

    /**
     * 用户集合
     */
    public static ArrayList<User> USERS = new ArrayList<>();

    /**
     * 配置文件地址
     */
    public static final String SETTING_FILE_PATH = System.getProperty("user.dir") + "\\wifiConfig.setting";

    /**
     * 日志文件地址
     */
    public static final String LOG_FILE_PATH = System.getProperty("user.dir") + "\\WIFI日志.txt";

    /**
     * 读取配置文件
     */
    public static Setting setting = new Setting(FileUtil.touch(SETTING_FILE_PATH), CharsetUtil.CHARSET_UTF_8, false);

    /**
     * 推送熊的UUID
     */
    public static String UUID = "";

    /**
     * 选择推送方式
     */
    public static String DELIVERY_METHOD = "";

    public static String getUUID() {

        if ("".equals(UUID)) {
            UUID = setting.getStr("UUID");
        }
        return UUID;
    }

    public static String getDeliveryMethod() {

        if ("".equals(DELIVERY_METHOD)) {
            DELIVERY_METHOD = setting.getStr("推送方式");
        }
        return DELIVERY_METHOD;
    }

    /**
     * 获取口令
     *
     * @param isNeedReLogin 是否需要登录获取
     * @return 口令
     */
    public static String getStok(boolean isNeedReLogin) {

        if (Objects.equals(STOK, "") || isNeedReLogin) {
            STOK = MyUtils.getStok();

        }
        return STOK;
    }

    /**
     * 获取总时长
     *
     * @return the total duration
     */
    public static int getTotalDuration() {

        if (TOTAL_DURATION == -1) {
            TOTAL_DURATION = setting.getInt("总时长");
        }
        return TOTAL_DURATION;
    }

    /**
     * Gets password.
     *
     * @return the password
     */
    public static String getPassword() {

        if (Objects.equals(PASSWORD, "")) {
            PASSWORD = setting.getStr("密码");
        }
        return PASSWORD;
    }

    /**
     * Gets device code.
     *
     * @return the device code
     */
    public static String getDeviceCode() {

        if (Objects.equals(DEVICE_CODE, "")) {
            DEVICE_CODE = setting.getStr("设备码");
        }
        return DEVICE_CODE;
    }

    /**
     * Gets ip.
     *
     * @return the ip
     */
    public static String getIp() {

        if (Objects.equals(IP, "")) {
            IP = setting.getStr("服务器IP");
        }
        return IP;
    }

    /**
     * Gets query time interval.
     *
     * @return the query time interval
     */
    public static int getQueryTimeInterval() {

        if (QUERY_TIME_INTERVAL == -1) {
            QUERY_TIME_INTERVAL = setting.getInt("时间间隔");
        }
        return QUERY_TIME_INTERVAL;
    }

    /**
     * Gets users.
     *
     * @return the users
     */
    public static ArrayList<User> getUsers() {

        if (USERS.size() == 0) {
            int i = 0;
            while (true) {
                String username = setting.getSetting("用户" + (++i)).getStr("用户名");
                String deviceName = setting.getSetting("用户" + (i)).getStr("设备名");
                if (username != null && deviceName != null) {
                    USERS.add(new User(username, deviceName));
                } else {
                    break;
                }
            }
        }
        return USERS;
    }

}
